<?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: 流年 <liu21st@gmail.com>
    // +----------------------------------------------------------------------

    use Jrk\Email;
    use Jrk\Times;
    use think\facade\Cache;
    use think\facade\Env;

    //加载额外的
    if (is_file(Env::get('app_path').'function.php')) {
        include_once Env::get('app_path').'function.php';
    }

    // 应用公共文件

    if (!function_exists('rm_empty_dir')) {
        /**
         * @param $path
         * @return bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: rm_empty_dir
         * @describe:删除所有空目录
         */
        function rm_empty_dir($path)
        {
            if (!(is_dir($path) && ($handle = opendir($path)) !== false)) {
                return false;
            }
            while (($file = readdir($handle)) !== false) {
                if (!($file != '.' && $file != '..')) {
                    continue;
                }
                $curfile = $path.DS_PROS.$file;// 当前目录
                if (!is_dir($curfile)) {
                    continue;
                }
                rm_empty_dir($curfile);
                if (count(scandir($curfile)) == 2) {
                    rmdir($curfile);
                }
            }
            closedir($handle);
        }
    }


    if (!function_exists('hook')) {
        /**
         * @param $behavior
         * @param $params
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: hook
         * @describe:钩子行为
         */
        function hook($behavior, $params)
        {
            think\facade\Hook::exec($behavior, $params);
        }
    }


    if (!function_exists('sendMaile')) {
        /**
         * @param array $arr
         * @return bool|string
         * @throws \PHPMailer\PHPMailer\Exception
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: sendMaile
         * @describe: 邮件发送
         */
        function sendMaile($arr = [])
        {
            if (empty($arr)) {
                return false;
            }
            $maile = Email::instance();
            $maile->subject($arr['subject']);
            $maile->body($arr['body']);
            $maile->to($arr['to']);

            if (isset($arr['attachment'])) {
                if (is_array($arr['attachment'])) {
                    foreach ($arr['attachment'] as $v) {
                        $maile->attachment($v);
                    }
                } else {
                    $maile->attachment($arr['attachment']);
                }
            }
            $result = $maile->send();
            if ($result) {
                return $result;
            } else {
                return $maile->getError();
            }
        }
    }


    if (!function_exists("formBytes")) {
        /**
         * @param $size
         * @param string $delimiter
         * @return string
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: format_bytes
         * @describe:格式化数据
         */
        function formBytes($size, $delimiter = '')
        {
            $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
            for ($i = 0; $size >= 1024 && $i < 5; $i++) {
                $size /= 1024;
            }

            return round($size, 2).$delimiter.$units[$i];
        }
    }

    if (!function_exists("FriendTime")) {
        /**
         * @param $time
         * @return false|string
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/28
         * @name: FriendTime
         * @describe:时间友好
         */
        function FriendTime($time)
        {
            return Times::friendlyDate($time);
        }
    }

    if (!function_exists("clearCache")) {

        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/14
         * @name: clearCache
         * @describe:清除缓存
         */
        function clearCache()
        {
            Cache::clear();
        }
    }
